#!/bin/bash
## name : generate-network-config-file.sh
## desc : 直接生成区块链网络的配置文件
## create ：2020.4.8
## update : 2020.5.29



if [ -f "blockchain-network-config.yaml" ];then
	rm blockchain-network-config.yaml
fi

touch blockchain-network-config.yaml

echo "name: "Network-Config-Test"
x-type: "hlfv1"
description: "The network used in the integration tests"
version: 1.0.0" > blockchain-network-config.yaml

echo "" >>blockchain-network-config.yaml

echo "client:
  organization: OrgA
  credentialStore:
    path: "/tmp/hfc-kvs"

    cryptoStore:
      path: "/tmp/hfc-cvs"
    wallet: wallet-name" >>blockchain-network-config.yaml

echo "" >>blockchain-network-config.yaml

echo "channels:
  txchannel:
    orderers:
      - Orderer0
    peers:
      peer0.orga.example.com:
        endorsingPeer: true
        chaincodeQuery: true
        ledgerQuery: true
        eventSource: true

    chaincodes:
      - supermapcc" >>blockchain-network-config.yaml

echo "" >>blockchain-network-config.yaml

echo "organizations:
  OrgA:
    mspid: OrgA
    peers:
      - peer0.orga.example.com

    adminPrivateKey:
      pem: | " >>blockchain-network-config.yaml

	  PrivateKey=$(ls $PWD/crypto-config/peerOrganizations/orga.example.com/users/Admin\@orga.example.com/msp/keystore/*)
    while read line
    do
        echo "        $line" >> blockchain-network-config.yaml
    done < $PrivateKey

    echo "" >>blockchain-network-config.yaml

    echo "    signedCert:
      pem: | " >>blockchain-network-config.yaml
	  signedCert=$(ls $PWD/crypto-config/peerOrganizations/orga.example.com/users/Admin\@orga.example.com/msp/signcerts/Admin\@orga.example.com-cert.pem)
    while read line
    do
        echo "        $line" >> blockchain-network-config.yaml
    done < $signedCert

echo "" >>blockchain-network-config.yaml

echo "    certificateAuthorities:
      - ca-orgA">>blockchain-network-config.yaml


echo "" >>blockchain-network-config.yaml

echo "orderers:
  Orderer0:
    url: grpcs://orderer0.example.com:7050
    tlsCACerts:
      pem: |" >>blockchain-network-config.yaml

    orderertlsCACerts=$(ls $PWD/crypto-config/ordererOrganizations/example.com/tlsca/*.pem)
    while read line
    do
        echo "        $line" >> blockchain-network-config.yaml
    done < $orderertlsCACerts

    echo "    grpcOptions:
      grpc-max-send-message-length: 15
      grpc.keepalive_time_ms: 360000
      grpc.keepalive_timeout_ms: 180000" >>blockchain-network-config.yaml

echo "" >>blockchain-network-config.yaml

echo "peers:
  peer0.orga.example.com:
    url: grpcs://peer0.orga.example.com:7051
    tlsCACerts:
      pem: |" >>blockchain-network-config.yaml
    peertlsCACerts=$(ls $PWD/crypto-config/peerOrganizations/orga.example.com/tlsca/*.pem)
    while read line
    do
        echo "        $line" >> blockchain-network-config.yaml
    done < $peertlsCACerts

    echo "    grpcOptions:
      grpc.http2.keepalive_time: 15" >>blockchain-network-config.yaml

echo "" >>blockchain-network-config.yaml


echo "certificateAuthorities:
  ca-orgA:
    url: http://ca.orga.example.com:7054
    registrar:
      enrollId: admin
      enrollSecret: adminpw
    caName: ca">>blockchain-network-config.yaml

echo "" >>blockchain-network-config.yaml
echo "externalStore: hbase" >>blockchain-network-config.yaml

echo "" >>blockchain-network-config.yaml
echo "hbase:
  address:
    <ip address>
  port:
    2181">>blockchain-network-config.yaml

